package com.android.ContactPresets;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.ServiceManagerNative;
import android.telephony.IPhone;

public class TimeoutService extends Service {

	NotificationManager mNM;

	@Override
	public IBinder onBind(Intent intent)
	{
		return mBinder;
	}

	protected void onCreate()
	{
		mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

		// show the icon in the status bar
		showNotification();

		// Start up the thread running the service.  Note that we create a
		// separate thread because the service normally runs in the process's
		// main thread, which we don't want to block.
		Thread thr = new Thread(null, mTask, "TimeoutService");
		thr.start();
	}

	protected void onDestroy()
	{
		//If the call is not on i.e phone is onhook cancel the alarm
		try{
			IPhone	phoneService = IPhone.Stub.asInterface(ServiceManagerNative.getDefault().getService("phone"));
			if (phoneService.isOffhook()==false && phoneService != null){
				AlarmManager am  = (AlarmManager)getSystemService(ALARM_SERVICE);
				Intent intent = new Intent(this, TimeoutIntentReceiver.class);
				am.cancel(intent);
			}
		}catch(Exception e){}
		// Cancel the notification -- we use the same ID that we had used to start it
		mNM.cancel(12345);
	}

	/**
	 * The function that runs in our worker thread
	 */
	Runnable mTask = new Runnable() {
		public void run()
		{
			//Sleep for 10 seconds.
			long endTime = System.currentTimeMillis() + 10000;
			while (System.currentTimeMillis() < endTime) {
				synchronized (mBinder) {
					try {
						mBinder.wait(endTime - System.currentTimeMillis());
					} catch (Exception e) {
					}
				}
			}
			//Done with our work...  stop the service!
			// TODO Need to find a better implementation of stop/start service.

			TimeoutService.this.stopSelf();
		}
	};

	private void showNotification() {
		Intent appIntent = new Intent(this, TimeoutService.class);
		Intent contentIntent = new Intent(this, TimeoutService.class);
		mNM.notify(12345, new Notification(     
				this,                        // our context
				R.drawable.clockicon ,    // the icon for the status bar
				getText(R.string.timeout_msg),      // the text to display in the ticker
				System.currentTimeMillis(),  // the timestamp for the notification
				getText(R.string.timeout_msg), // the title for the notification
				getText(R.string.timeout_msg),    // the details to display in the notification
				contentIntent,               // the contentIntent (see above)
				R.drawable.contactsicon,  // the app icon
				getText(R.string.app_name), // the name of the app
				appIntent));                 // the appIntent (see above)));
	}

	/**
	 * This is the object that receives interactions from clients.  
	 */
	private final IBinder mBinder = new Binder()
	{
		@Override
		protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) {
			return super.onTransact(code, data, reply, flags);
		}
	};
}
